LeetCode LCP 50. 宝石补给

Racket

解法一

(define (give-gem-internal gem operations)
  ;; (displayln gem)
  (if (null? operations) gem
      (let* ([operation (car operations)]
             [from (car operation)]
             [to (cadr operation)]
             [value (floor (/ (car (list-ref gem from)) 2))])
        (give-gem-internal
         (map (lambda (x) (cond [(equal? (cadr x) from) (list (- (car x) value) (cadr x))]
                                [(equal? (cadr x) to) (list (+ (car x) value) (cadr x))]
                                [else x])) gem)
         (cdr operations)))))
   
(define/contract (give-gem gem operations)
  (-> (listof exact-integer?) (listof (listof exact-integer?)) exact-integer?)
  (let ([result (map car (give-gem-internal
                 (map list gem (range (length gem)))
                 operations))])
    (- (apply max result) (apply min result))
  ))

解法2

gem list->vector,遍历 operations 加加减减即可。


本文作者:Maeiee

本文链接:LeetCode LCP 50. 宝石补给

版权声明:如无特别声明,本文即为原创文章,版权归 Maeiee 所有,未经允许不得转载!


喜欢我文章的朋友请随缘打赏,鼓励我创作更多更好的作品!